<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="盘点状态确认" prop="dutyResultStatus">
        <el-select v-model="formItem.dutyResultStatus" clearable placeholder="请选择状态" style="width:100%">
          <el-option v-for="item in inventoryResultStatus" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  status,
  updateStatus,
  detail
} from '@neu/dcam-ui/api/inventoryResult'

export default {
  name: 'InventoryResultConfirmForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 结果状态
      inventoryResultStatus: [],
      formItem: {
        dutyResultStatus: ''
      },
      // 表单验证规则
      rules: {
        dutyResultStatus: [{
          required: true,
          message: '确认状态不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    // 获取盘点结果状态
    status().then(res => { this.inventoryResultStatus = res.data })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          dutyResultStatus: ''
        }
      }
    },
    /** 结果状态确认 */
    onOk() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(updateStatus(this.id, this.formItem.dutyResultStatus))
            }
          }
        })
      })
    },
    // 重置表单
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
